package hy.controller;

import hy.beans.Product;
import hy.beans.Result;
import hy.service.ProductService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService service;
    @RequestMapping("/findProducts")
    public List<Product> getAllProducts() {
        return service.getAllProducts();
    }
    @RequestMapping("/addCart")
    public Result addCart(String phone, String productId) {
        return service.addCart(phone, productId);
    }
    @RequestMapping("/createOrder")
    public Result createOrder(String phone) {
        Result r = new Result();
        String rs;
        try {
            rs = service.createOrder(phone);
        } catch (Exception e) {
            e.printStackTrace();
            rs = e.getMessage();
        }
        if (!rs.equals("success")) {
            r.setStorage(rs);
            return r;
        }
        /*<dependency>
            <groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.9</version>
		</dependency>
		产生随机订单号,true表示包含字母和数字
		*/
        String no = RandomStringUtils.random(5, true, true);
        r.setStorage(no);
        r.setSuccess(true);
        return r;
    }
}